﻿using System;
using System.Collections.ObjectModel;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;

namespace EventLogAnalyzer.Model
{
    public class EventLogAnalyzerModel : BusinessModelBase
    {
        public EventLogAnalyzerModel()
        {
            CommonEvents = new ObservableCollection<CommonEvent>();
            LogFiles = new ObservableCollection<LogFile>();
        }

        public void ParseFiles(string[] filePaths)
        {
            foreach (var filePath in filePaths)
            {
                var logFile = new LogFile{FileName = filePath};
                LogFiles.Add(logFile);
                var reader = new XmlTextReader(new StreamReader(filePath, Encoding.GetEncoding("ISO-8859-1")));
                
                string currentTag = string.Empty;
                string currentEventData = string.Empty;
                DateTime timeStamp = DateTime.MinValue;
                reader.XmlResolver = null;
                while (reader.Read())
                 {
                    switch (reader.NodeType) 
                    {
                        case XmlNodeType.Element:
                            currentTag = reader.Name;
                            if (currentTag == "TimeCreated")
                                timeStamp = DateTime.Parse(reader.GetAttribute("SystemTime"));
                            break;
                        case XmlNodeType.CDATA:
                        case XmlNodeType.Text:
                            if (currentTag == "Data" || currentTag == "Message")
                                currentEventData = reader.Value;
                            else if (currentTag == "TimeGenerated")
                            {
                                var timeStampString = reader.Value.Substring(0, reader.Value.IndexOf("."));
                                timeStamp = DateTime.ParseExact(timeStampString, "yyyyMMddHHmmss", new CultureInfo("en-US"));
                            }

                            break;
                        case XmlNodeType.EndElement:
                            if (reader.Name == "Event" || reader.Name == "REC")
                                AnalyseEventData(currentEventData, logFile, timeStamp);
                            break;
                    }
                }
            }
        }

        private void AnalyseEventData(string currentEventData, LogFile logFile, DateTime timeStamp)
        {
            var lineLenght = 200;
            var startIndex = currentEventData.IndexOf(' ', currentEventData.IndexOf(' ', currentEventData.IndexOf(' ', currentEventData.IndexOf(' ', currentEventData.IndexOf(' ', 0)+1)+1)+1)+1);
            var commonPart = startIndex < 0 ? currentEventData : currentEventData.Substring(startIndex);

            var matchingEvent = CommonEvents.FirstOrDefault(e => e.CommonPart == commonPart);
            if (matchingEvent != null)
                matchingEvent.NumberOfOccurances++;
            else
            {
                matchingEvent = new CommonEvent { EventTitle = commonPart.Length > lineLenght? commonPart.Substring(0, lineLenght): commonPart.Substring(0, commonPart.Length), EventDescription = currentEventData, NumberOfOccurances = 1, CommonPart = commonPart};
                CommonEvents.Add(matchingEvent);
            }
            logFile.Logs.Add(new EventLog { CommonEventId = matchingEvent.Id, Data = currentEventData, TimeStamp = timeStamp});
        }

        public ObservableCollection<CommonEvent> CommonEvents { get; set; }
        public ObservableCollection<LogFile> LogFiles { get; set; }

    }
}